package com.xinyi.tangmi;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.R.integer;
import android.os.Handler;
import android.util.Log;

public class GetAutoItems {
	private static final int MSG_ITEMDETAIL = 54;
	List<AutoMatchItem> items;
	private String PATH;
	private Handler handler;
	RequestContent requestContent;

	public GetAutoItems(final Handler handler) throws Exception {
		// TODO Auto-generated constructor stub
		this.handler = handler;
		PATH = ContentFragment.AUTO_PATH + "&page_size="
				+ MainActivity.AUTO_PAGE_SIZE + "&page="
				+ MainActivity.autoPage;
		Log.i("path", PATH + "..");
	}

	public List<AutoMatchItem> getItems() {
		List<AutoMatchItem> items = new ArrayList<AutoMatchItem>();
		AutoMatchItem autoMatchItem = new AutoMatchItem();
		try {
			JSONObject jsonObject = readJason(PATH, handler);
			if (!jsonObject.getString("error").equals("0")) {
				return items;
			}
			JSONObject object;
			JSONArray jsonArray = jsonObject.getJSONArray("content");
			for (int i = 0; i < jsonArray.length(); i++) {
				autoMatchItem = new AutoMatchItem();
				object = (JSONObject) jsonArray.get(i);
				autoMatchItem.setShangyiUrl(ContentFragment.PIC_PATH
						+ object.getString("photos"));
				autoMatchItem.setShangyiId(object.getString("auto_id"));
				autoMatchItem.setShuoming(object.getString("auto_desc"));
				JSONArray array = object.getJSONArray("goods_link_id");
				object = array.getJSONObject(0);
				autoMatchItem.setBeibaoUrl(ContentFragment.PIC_PATH
						+ object.getString("goods_thumb"));
				autoMatchItem.setBeibaoId(object.getString("goods_id"));
				object = array.getJSONObject(1);
				autoMatchItem.setXiazhuangUrl(ContentFragment.PIC_PATH
						+ object.getString("goods_thumb"));
				autoMatchItem.setXiazhuangId(object.getString("goods_id"));
				object = array.getJSONObject(2);
				autoMatchItem.setXieziUrl(ContentFragment.PIC_PATH
						+ object.getString("goods_thumb"));
				autoMatchItem.setXieziId(object.getString("goods_id"));
				object = array.getJSONObject(3);
				autoMatchItem.setShuoshiUrl(ContentFragment.PIC_PATH
						+ object.getString("goods_thumb"));
				autoMatchItem.setShuoshiId(object.getString("goods_id"));
				object = array.getJSONObject(4);
				autoMatchItem.setYanjingUrl(ContentFragment.PIC_PATH
						+ object.getString("goods_thumb"));
				autoMatchItem.setYanjingId(ContentFragment.PIC_PATH
						+ object.getString("goods_id"));
				items.add(autoMatchItem);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return items;

	}

	public JSONObject readJason(String urlString, Handler handler)
			throws Exception {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		URL url = new URL(urlString);
		HttpURLConnection hUrlConnection = (HttpURLConnection) url
				.openConnection();
		if (hUrlConnection.getResponseCode() == 200) {
			InputStream inputStream = hUrlConnection.getInputStream();
			Log.i("code", hUrlConnection.getResponseCode() + "");
			int len = 0;
			while ((len = inputStream.read(data)) != -1) {
				byteArrayOutputStream.write(data, 0, len);
			}
			inputStream.close();
			byte[] bytes = byteArrayOutputStream.toByteArray();
			JSONObject jsonObject = new JSONObject(new String(bytes));
			return jsonObject;
		}
		return null;

	}

}